#include "mainWin.h"
#include "gl.h"

#include <cstdio>

window* win = NULL;

static LRESULT CALLBACK mainProc(HWND,UINT,WPARAM,LPARAM);

mainWin::mainWin(): window(),
                    bkg( this ),
                    frm( this ),
                    bar( this ),
                    bsel( this )
{
    callback = (WNDPROC)mainProc;
    create();

    rm.add(ft = new font());
    ft->setValidity(RES_ESSENTIAL);
    ft->create();

    rm.add(sSheet = new styleSheet());
    sSheet->setColor(ELTY(elm_fieldset,bkg_st_normal),new color(col_li_red));
    sSheet->setColor(ELTY(elm_frame,bkg_st_normal),new color(col_black));
    sSheet->setColor(ELTY(elm_frame,bkg_st_active),new color(col_dk_grey));
    sSheet->setColor(ELTY(elm_winBar,bkg_st_normal),new color(col_li_blue));
    sSheet->setColor(ELTY(elm_winBar,bkg_st_active),new color(col_li_green));
    sSheet->setColor(ELTY(elm_button,bkg_st_normal),new color(col_dk_red));
    sSheet->setColor(ELTY(elm_button,bkg_st_hover),new color(col_li_red));
    sSheet->setColor(ELTY(elm_toggleButton,bkg_st_normal),new color(col_md_grey));
    sSheet->setColor(ELTY(elm_toggleButton,bkg_st_hover),new color(col_md_green));
    sSheet->setColor(ELTY(elm_toggleButton,bkg_st_on),new color(col_dk_green));
    sSheet->setColor(ELTY(elm_winBar,txt_st_normal),new color(col_white));
    sSheet->setColor(ELTY(elm_winBar,txt_st_active),new color(col_black));
    sSheet->setColor(ELTY(elm_barSelector,bkg_st_normal),new color(0xff,0xff,0x00));
    sSheet->setColor(ELTY(elm_barSelector,bkg_st_active), new color(0xcc,0xcc,0x00));
    sSheet->setColor(ELTY(elm_barSelCursor,bkg_st_normal),new color(0x00,0xff,0x00));
    sSheet->setValidity(RES_ESSENTIAL);

    bkg.setStyle(sSheet);
    bkg.clipToWindow();
    bkg.setInactivable(true);

    frm.setStyle(sSheet);
    frm.setRect( 100,100,300,300 );
    frm.setBar(&bar);

    bar.setStyle( sSheet );
    bar.setMode(WBAR_CLOSE_BTN|WBAR_MINI_BTN|WBAR_LOCK_BTN|WBAR_CAPTION);
    bar.setFont(ft);
    bar.setCaption("Frame - Ado 3.14 on its way !");

    bsel.setStyle( sSheet );
    bsel.setBounds( 0, 100 );
    bsel.setRect( 5,5, 100, 5 );
    bsel.getCursorElem()->setRect(0,0,20,5);
    bsel.getCursorElem()->setStyle( sSheet );
    bsel.setParentSet(&frm);

    td.setFont(ft);

    tim.start();
}

mainWin::~mainWin(){}

void        mainWin::drawGLScene(){
	window::drawGLScene();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear The Screen And The Depth Buffer
	glLoadIdentity();							// Reset The Current Modelview Matrix

    elDraw();

    glColor3f(1.0f,1.0f,1.0f);
    glRasterPos(5,5);

    char buffer[255];
    sprintf(buffer,">>MainWindow %ld : %ld",tim.getCurrentTime() / CLOCKS_PER_MS,bsel.getValue());
    td.setCaption(string(buffer));
    td.display(0,0);

}

font* mainWin::getFont(){
    setCurrent();
    return ft;
}

static LRESULT CALLBACK mainProc(	HWND	hWnd,					// Handle For This Window
				UINT	uMsg,					// Message For This Window
				WPARAM	wParam,					// Additional Message Information
				LPARAM	lParam)					// Additional Message Information
{
    if( !win ) return DefWindowProc(hWnd,uMsg,wParam,lParam);
	switch (uMsg){
		case WM_ACTIVATE:{
			if (!HIWORD(wParam)){
				win->setActive(true);					// Program Is Active
			}
			else{
				win->setActive(false);					// Program Is No Longer Active
			}
			return 0;						// Return To The Message Loop
		}
		case WM_SYSCOMMAND:{
			switch (wParam)						// Check System Calls
			{
				case SC_SCREENSAVE:				// Screensaver Trying To Start?
				case SC_MONITORPOWER:				// Monitor Trying To Enter Powersave?
				return 0;					// Prevent From Happening
			}
			break;							// Exit
		}
		case WM_CLOSE:{
			PostQuitMessage(0);					// Send A Quit Message
			return 0;						// Jump Back
		}
		case WM_SIZE:{
			win->resizeGLScene(LOWORD(lParam),HIWORD(lParam));
			return 0;						// Jump Back
		}
	}

	if( !win->getInput()->translateInputMsg(uMsg,wParam,lParam) ) return 0;

	// Pass All Unhandled Messages To DefWindowProc
	return DefWindowProc(hWnd,uMsg,wParam,lParam);
}

void mainWin::eventListener( event* ev ){
    event_type type = ev->getType();
    if( type == event_type_window_resize ){
        bkg.clipToWindow();
    }
    else{
        window::eventListener( ev );
        return;
    }
    delete ev;
}
